package com.gitee.melin.bee.core.jdbc.relational;

import java.io.Serializable;
import java.sql.JDBCType;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class MetaColumn implements Serializable, Comparable<MetaColumn> {

    private static final long serialVersionUID = 6438514547501611599L;

    private String columnName; // 小写

    // e.g. `varchar(10)` for MySQL
    private String columnType;

    // e.g. `varchar` for MySQL
    private String dataType;

    private JDBCType jdbcType;

    private String comment;

    private boolean primaryKey;

    private boolean partitionField; // hive

    private boolean autoIncrement;

    private Object defaultValue;

    private Boolean nullable = true;

    private String columnDefinition;

    private Integer position;

    // e.g. `varchar` length is 10
    private Long length;

    private Long maxLength;

    // e.g. `decimal(10, 2)` precision is 10
    private Long precision;

    // e.g. `decimal(10, 2)` scale is 2 or timestamp(6) scale is 6
    private Integer scale;

    // e.g. `tinyint unsigned` is true
    protected boolean unsigned;

    public String getColumnType() {
        if (columnType == null) {
            return dataType;
        } else {
            return columnType;
        }
    }

    public String getTypeName() {
        if (StringUtils.isBlank(dataType)) {
            return dataType;
        }

        if (length != null) {
            return dataType + "(" + length + ")";
        }

        if ("number".equalsIgnoreCase(dataType) && precision != null) {
            return dataType + "(" + precision + ")";
        }

        return dataType;
    }

    @Override
    public int compareTo(MetaColumn column) {
        if (this.position != null && column.position != null) {
            return this.position.compareTo(column.position);
        } else {
            return this.columnName.compareTo(column.columnName);
        }
    }

    @Override
    public String toString() {
        return "MetaColumn{"
                + "scale="
                + scale
                + ", precision="
                + precision
                + ", columnLength="
                + length
                + ", position="
                + position
                + ", columnDefinition='"
                + columnDefinition
                + '\''
                + ", nullable="
                + nullable
                + ", defaultValue='"
                + defaultValue
                + '\''
                + ", autoIncrement="
                + autoIncrement
                + ", partitionField="
                + partitionField
                + ", primaryKey="
                + primaryKey
                + ", comment='"
                + comment
                + '\''
                + ", jdbcType="
                + jdbcType
                + ", columnType='"
                + dataType
                + '\''
                + ", columnName='"
                + columnName
                + '\''
                + '}';
    }
}
